$(document).ready(function() {
//--用户----------------------------------------------------------------------------------------------------
//	用户添加 
	var addIdentity = "teacher";
	$('#user-add-type label').click(function() { addIdentity = $(this).text(); });
	$('#user-add-enter').click(function() {
		if (checkInput('#user-add-ID', '#user-add-password', '#alert-user-add', "请将注册信息填写完整")) {
			$.post("",
				{
					ID: $('#user-add-ID').val(),
					identity: addIdentity,
					password: $('#user-add-password').val()
				}, 
				function(json) {
					if (json.result == "success") {
						showAlert('#alert-user-add', "alert-success", "用户"+$('#user-add-ID').val()+"注册成功");
					} else {
						showAlert('#alert-user-add', "alert-danger", json.result);
					}
				}
			);
		}
	});
//	获取用户列表
	function getUserList(pageNow) {
		$.post("",
			{
				page: pageNow,
				ID: $('#search-user-ID').val()
			},
			function(json) {
				$('#table-user-list').empty();
				if (json.result == "empty") {
					var tr = "tr><td></td><td>暂无此用户</td><td></td></tr>";
					$('#table-user-list').append(tr);
				} else {
					for (var i = 0; i < json.data.length; ++i) {
						var tr = "<tr onclick=\"modal_ID='"+json.data[i].ID+"';modal_identity='"
							+json.data[i].identity+"';\" data-toggle=\"modal\" data-target=\"#modal-user-info\">\<td>"
							+json.data[i].ID+"</td><td>"+json.data[i].identity+"</td><td>"
							+json.data[i].time+"</td><td>更多</td></tr>";
						$('#table-user-list').append(tr);
					}
					if (json.pageNum > 1) {
						makePaginator('#user-paginator', pageNow, 9, json.pageNum);
						$('#user-paginator li a').click(function() {
							getUserList($('#user-paginator li[class=active] a').text());
						})
					}
				}
			}
		);
	}
	$('#search-user-enter').click(function() {
		getUserList(1);
	});
	$('#table-user-list tr').click(function() {
		$('#modal-user-title').text(modal_identity+" "+modal_ID);
		$.post("",
			{
				ID: modal_ID
			},
			function(json) {
				$('#update-user-nickname').val(json.nickname);
				$('#update-user-signature').val(json.signature);
				$('#update-user-name').val(json.name);
				if(json.sex=="男"){$('#update-user-male').addClass("active");updateSex="男";}
				if(json.sex=="女"){$('#update-user-female').addClass("active");updateSex="女";}
				$('#update-user-ID').val(json.ID);
				$('#update-user-school').val(json.school);
				$('#update-user-class').val(json.class);
				$('#update-user-email').val(json.email);
				for (var i = 0; i < json.school.length; ++i) {
					var li = "<li><a href=\"#\">"+json.school[i].name+"</a></li>";
					$('#user-list-school').prepend(li);
				}
				for (var i = 0; i < json.class.length; ++i) {
					var li = "<li><a href=\"#\">"+json.class[i].name+"</a></li>";
					$('#user-list-class').prepend(li);
				}
			}
		);
	});
//	变相实现selectmenu
	$('#user-list-school li').click(function() {
		$('#update-user-school').text($('#user-list-school li').text());
	});
	$('#user-list-class li').click(function() {
		$('#update-user-class').text($('#user-list-class li').text());
	});
	$('#update-user-email-goto').click(function() {
		$('#modal-user-secure').tab("show");
	});
//	更改用户信息
	var updateSex = "";
	$('#update-user-sex label').click(function() { updateSex = $(this).text(); });
	$('#update-user-info').click(function() {
		$.post("",
			{
				nickname: $('#update-user-nickname').val(),
				signature: $('#update-user-signature').val(),
				name: $('#update-user-name').val(),
				sex: updateSex,
				ID: $('#update-user-ID').val(),
				school: $('#update-user-school').text(),
				class: $('#update-user-class').text(),
				email: $('#update-user-email').val()
			},
			function(json) {
				if (json.result == "success") {
					showAlert('#alert-user-info', "alert-success", "保存成功");
				} else {
					showAlert('#alert-user-info', "alert-danger", json.result);
				}
			}
		);
	});
//	更改用户密码
	$('#update-user-secure').click(function() {
		if (checkInput('#update-user-password', 'update-user-password-check', '#alert-user-secure', "请输入全部信息")) {
			if ($('#update-user-password').val() != $('#update-user-password-check').val()) {
				showAlert('#alert-user-secure', "alert-danger", "两次密码输入不一致");
				return;
			}
			$.post("",
				{
					ID: modal_ID,
					password: $('#update-user-password').val()
				},
				function(json) {
					if (json.result == "success") {
						showAlert('#alert-user-secure', "alert-success", "密码修改成功");
					} else {
						showAlert('#alert-user-secure', "alert-danger", json.result);
					}
				}
			)
		}
	})
//	删除用户
	$('#update-user-delete').click(function() {
		$.post("",
			{
				ID: modal_ID
			},
			function(json) {
				if (json.result == "success") {
					showAlert('#alert-user-search', "alert-success", "用户删除成功");
				} else {
					showAlert('#alert-user-search', "alert-danger", json.result);
				}
			}
		)
	});
//---------------------------------------------------------------------------------------------------------

//--题目----------------------------------------------------------------------------------------------------
	$('#problem-list-point li').click(function() {
		$('#problem-add-point').text($('#problem-list-point li').text());
	});
	$('#modal-problem-list-point li').click(function() {
		$('#update-problem-point').text($('#modal-problem-list-point li').text());
	});
	var addType = "";
	$('#problem-add-type label').click(function() { addType = $(this).text(); });
	var addLevel = "";
	$('#problem-add-level label').click(function() { addLevel = $(this).text() });
	var updateType = "";
	$('#update-problem-type label').click(function() { updateType = $(this).text(); });
	var updateLevel = "";
	$('#update-problem-level label').click(function() { updateLevel = $(this).text() });
});